package com.firebird.app.fekpsl.ui.activity.me

import android.app.DatePickerDialog
import android.view.View
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.base.BaseMvpActivity
import com.firebird.app.fekpsl.constant.Constant
import com.firebird.app.fekpsl.ext.formatCurrentDate
import com.firebird.app.fekpsl.ext.stringToCalendar
import com.firebird.app.fekpsl.mvp.me.BirthdayContract
import com.firebird.app.fekpsl.mvp.me.BirthdayPresenter
import com.firebird.app.fekpsl.utils.Preference
import kotlinx.android.synthetic.main.me_update_birthday.*
import kotlinx.android.synthetic.main.toolbar.*
import java.util.*

class UpdateBirthdayActivity  : BaseMvpActivity<BirthdayContract.View, BirthdayContract.Presenter>(), BirthdayContract.View {
    private var pf_birthday:String by Preference(Constant.BIRTHDAY_KEY, "")
    private var pf_uuid:String by Preference(Constant.UUID_KEY, "")
    /**
     * Date
     */
    private var mCurrentDate = formatCurrentDate()

    override fun createPresenter(): BirthdayContract.Presenter = BirthdayPresenter()

    private val mDialog by lazy {
        // DialogUtil.getWaitDialog(this, getString(R.string.save_ing))
    }

    override fun showLoading() {
        // mDialog.show()
    }

    override fun hideLoading() {
        // mDialog.dismiss()
    }

    override fun updateBirthdaySuccess(success: Boolean) {
        if(success){
            pf_birthday=tv_birthday_date.text.toString()
            finish()
        }else{
            // showToast(getString(R.string.me_save_fail))
        }
    }
    /*
        override fun updateNiceNameFail() {
            showToast(getString(R.string.me_save_fail))
        }*/
    override fun attachLayoutRes(): Int = R.layout.me_update_birthday

    override fun initData() {
    }

    override fun initView() {
        super.initView()

        toolbar.run {
            title = resources.getString(R.string.me_update_birthday)
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
        }
        tv_birthday_date.setText(pf_birthday)

        ll_date.setOnClickListener {
            var now = Calendar.getInstance()
            now=pf_birthday.stringToCalendar()

            val dpd = android.app.DatePickerDialog(this,
                    DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
                        mCurrentDate = "$year-${month + 1}-$dayOfMonth"
                        tv_birthday_date.text = mCurrentDate
                    },
                    now.get(Calendar.YEAR),
                    now.get(Calendar.MONTH),
                    now.get(Calendar.DAY_OF_MONTH)
            )
            dpd.show()
        }

        btn_save.setOnClickListener(onClickListener)
    }

    override fun start() {
    }

    /**
     * OnClickListener
     */
    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.btn_save -> {
                updateBirthday()
            }
        }
    }
    private fun updateBirthday() {
        if (validate()) {
            mPresenter?.updateBirthday(pf_uuid,
                    tv_birthday_date.text.toString()
            )
        }
    }
    /**
     * check data
     */
    private fun validate(): Boolean {
        var valid = true
        val birthday: String = tv_birthday_date.text.toString()

        if (birthday.isEmpty()) {
            tv_birthday_date.error = getString(R.string.me_birthday_not_empty)
            valid = false
        }
        return valid
    }

}